package myComponent

import (
	"io"
	"log"
	"os"

	"gopkg.in/natefinch/lumberjack.v2"
)

// MultiWriter 实现了 io.Writer 接口，用于将数据写入多个 io.Writer
type MultiWriter struct {
	Writers []io.Writer
}

// 让 MultiWriter 实现 io.Writer 接口
func (mw *MultiWriter) Write(p []byte) (n int, err error) {
	for _, w := range mw.Writers {
		n, err = w.Write(p)
		if err != nil {
			return
		}
	}
	return len(p), nil
}

// 初始化日志相关配置
func LogInit() {
	multiWriter := &MultiWriter{
		Writers: []io.Writer{
			os.Stdout, // 标准输出
			&lumberjack.Logger{
				Filename:   "./log/qwq-ai.log",
				MaxSize:    100,
				MaxAge:     30,
				MaxBackups: 12,
				LocalTime:  true,
				Compress:   true,
			}, // 滚动日志
		},
	}
	log.SetOutput(multiWriter)
	log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)

}
